package com.lyl.syt.api;

import com.lyl.syt.common.exception.YyghException;
import com.lyl.syt.common.result.ResultCodeEnum;
import com.lyl.syt.common.utils.HttpRequestHelper;
import com.lyl.syt.common.utils.MD5;
import com.lyl.syt.modules.mp.service.HospitalSetService;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.Map;

@Component
public class BaseAPI {
    @Resource
    private HospitalSetService hospitalSetService;

    public Map<String, Object> prefixHandler(HttpServletRequest request) {
        Map<String, String[]> parameterMap = request.getParameterMap();
        Map<String, Object> paramMap = HttpRequestHelper.switchMap(parameterMap);

        //1. 获取医院传递过来的签名，签名进行md5加密
        String hospSign = (String) paramMap.get("sign");

        //2. 根据传递过来的医院编码，查询数据库，查询签名
        String hoscode = (String) paramMap.get("hoscode");
        String signKey = hospitalSetService.getSignKey(hoscode);

        //3. 把数据库中查询的签名进行md5加密
        String encrypt = MD5.encrypt(signKey);

        //4. 判断签名是否一致
        if (!encrypt.equals(hospSign)){
            throw new YyghException(ResultCodeEnum.SIGN_ERROR);
        }
        return paramMap;
    }
}
